package com.todo.biz.application.handler.review.content;

import com.todo.biz.application.entity.context.ContentReviewContext;
import com.todo.biz.application.entity.context.ContentReviewResult;
import com.todo.biz.application.handler.review.content.wordfilter.WordContext;
import com.todo.biz.application.handler.review.content.wordfilter.WordFilter;
import com.todo.biz.application.entity.enums.ContentReviewEnum;
import com.todo.common.utils.StringUtils;
import com.todo.biz.common.enums.ReviewLevelEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2024/6/12 14:27
 * @description
 */
@Slf4j
@Component
public class SensitiveWordsHandle extends AbstractContentReview {

    @Resource
    WordContext wordContext;


    @Override
    protected Boolean match(ContentReviewContext context) {
        if (StringUtils.isNotBlank(context.getContent())) {
            return true;
        }
        return false;
    }

    @Override
    protected ContentReviewResult doHandle(ContentReviewContext context) {
        String content = context.getContent();
        WordFilter filter = new WordFilter(wordContext);
        ContentReviewResult contentReviewResult = new ContentReviewResult();
        //如果有黑名单词汇
        if (filter.includeBlack(content)) {
            ContentReviewResult.FailedDetail failedDetail = new ContentReviewResult.FailedDetail();
            failedDetail.setOriginContext(content);
            failedDetail.setResultContext(filter.replace(content));
            failedDetail.setFailedReason(String.format("包含黑名单敏感词汇：%s", filter.blackWordList(content)));
            contentReviewResult.setFailedDetail(failedDetail);
            contentReviewResult.setReviewResult(true);
            contentReviewResult.setReviewLevelEnum(ReviewLevelEnum.C);
            return contentReviewResult;
        } else if (filter.include(content)) {
            ContentReviewResult.FailedDetail failedDetail = new ContentReviewResult.FailedDetail();
            failedDetail.setOriginContext(content);
            failedDetail.setResultContext(filter.replace(content));
            failedDetail.setFailedReason(String.format("包含敏感词汇且已过滤：%s", filter.blackWordList(content)));
            contentReviewResult.setFailedDetail(failedDetail);
            contentReviewResult.setReviewLevelEnum(ReviewLevelEnum.B);
        } else {
            ContentReviewResult.FailedDetail failedDetail = new ContentReviewResult.FailedDetail();
            failedDetail.setOriginContext(content);
            failedDetail.setResultContext(content);
            failedDetail.setFailedReason("不包含敏感词");
            contentReviewResult.setFailedDetail(failedDetail);
        }
        if (contentReviewResult.getReviewLevelEnum() == null) {
            contentReviewResult.setReviewLevelEnum(ReviewLevelEnum.A);
        }
        contentReviewResult.setReviewResult(true);
        return contentReviewResult;
    }

    @Override
    protected ContentReviewEnum getContentReviewEnum() {
        return ContentReviewEnum.SENSITIVE_WORDS;
    }
}
